package com.example.learnshadow;

import android.app.Activity;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Build;
import android.os.Bundle;
import android.text.TextUtils;

import androidx.annotation.Nullable;

import com.example.shadowcore.ContextTheme;
import com.example.shadowcore.constants.GlobalConstants;

/**
 * Author: chinadragon
 * Time: 2023/9/13 18:47
 */
public class PluginContainerActivity extends Activity {
    private String className = "com.example.plugina.MainActivity";
    ContextTheme mContextTheme;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if (!TextUtils.isEmpty(getIntent().getStringExtra(GlobalConstants.KEY_CLASSS_NAME))){
            className = getIntent().getStringExtra(GlobalConstants.KEY_CLASSS_NAME);
        }

        try {
            mContextTheme = (ContextTheme) getClassLoader().loadClass(className).newInstance();
            mContextTheme.attch(this);
            Bundle bundle = new Bundle();
            bundle.putString(GlobalConstants.KEY_ACCOUNT, "1234567890");
            mContextTheme.onCreate(bundle);

        } catch (Exception e) {
            throw new RuntimeException(e);
        }


    }

    @Override
    public ClassLoader getClassLoader() {
        return PluginManagerImpl.getInstance().getDexClassLoader();
    }

    @Override
    public Resources getResources() {
        return PluginManagerImpl.getInstance().getResources();
    }

    @Override
    public void startActivity(Intent intent) {
        String className1 = intent.getStringExtra(GlobalConstants.KEY_CLASSS_NAME);
        Intent intent1 = new Intent(this, PluginContainerActivity.class);

        intent1.putExtra(GlobalConstants.KEY_CLASSS_NAME, className1);
        super.startActivity(intent1);
    }

    @Override
    protected void onStart() {
        mContextTheme.onStart();
        super.onStart();
    }

    @Override
    protected void onRestart() {
        mContextTheme.onRestart();
        super.onRestart();
    }



    @Override
    protected void onResume() {
        mContextTheme.onResume();
        super.onResume();
    }

    @Override
    protected void onPause() {
        mContextTheme.onPause();
        super.onPause();
    }

    @Override
    protected void onStop() {
        mContextTheme.onStop();
        super.onStop();
    }

    @Override
    protected void onDestroy() {
        mContextTheme.onDestroy();
        super.onDestroy();
    }
}
